package com.wulis.provider.basic.web;

import static com.wulis.common.model.enums.VersionEnum.WEB_V_1_0;

import javax.validation.Valid;

import com.wulis.provider.basic.model.vo.QueryUserListVo;
import com.wulis.provider.basic.model.dto.QueryUserPageDto;
import com.wulis.provider.basic.model.dto.UpdateUserDto;
import com.wulis.provider.basic.model.vo.QueryUserVo;
import com.wulis.provider.basic.service.UserInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wulis.common.annotation.ApiVersion;
import com.wulis.common.model.dto.IdDto;
import com.wulis.common.support.BaseController;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import com.wulis.provider.basic.model.dto.InsertUserDto;
import com.wulis.provider.basic.model.vo.QueryUserPageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author WuliBao
 * @date 2020-05-21
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("web/basic/user/info")
@Api(tags = "基础信息管理:用户管理")
public class UserInfoController extends BaseController {
    private final UserInfoService userInfoService;
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryUserPage")
    @ApiOperation("查询用户列表(分页)")
    @PreAuthorize("@api.hasPermission('web:basic:userInfo:queryUserPage')")
    public ApiWrapper<IPage<QueryUserPageVo>> queryUserPage(@Valid QueryUserPageDto queryUserPageDto) {
        return ApiWrapMapper.success(userInfoService.queryUserPage(queryUserPageDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryUserList")
    @ApiOperation("查询用户列表(根据部门ID)")
    @PreAuthorize("@api.hasPermission('web:basic:userInfo:queryUserList')")
    public ApiWrapper<List<QueryUserListVo>> queryUserList(@Valid IdDto idDto) {
        return ApiWrapMapper.success(userInfoService.queryUserList(idDto.getId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryUser")
    @ApiOperation("查询用户")
    @PreAuthorize("@api.hasPermission('web:basic:userInfo:queryUser')")
    public ApiWrapper<QueryUserVo> queryUser(@Valid IdDto idDto) {
        return ApiWrapMapper.success(userInfoService.queryUser(idDto.getId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("insertUser")
    @ApiOperation("新增用户")
    @PreAuthorize("@api.hasPermission('web:basic:userInfo:insertUser')")
    public ApiWrapper<Integer> insertUser(@Valid @RequestBody InsertUserDto insertUserDto) {
        return ApiWrapMapper.success(userInfoService.insertUser(insertUserDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PutMapping("updateUser")
    @ApiOperation("修改用户")
    @PreAuthorize("@api.hasPermission('web:basic:userInfo:updateUser')")
    public ApiWrapper<Integer> updateUser(@Valid @RequestBody UpdateUserDto updateUserDto) {
        return ApiWrapMapper.success(userInfoService.updateUser(updateUserDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @DeleteMapping("deleteUser")
    @ApiOperation("删除用户")
    @PreAuthorize("@api.hasPermission('web:basic:userInfo:deleteUser')")
    public ApiWrapper<Integer> deleteUser(@Valid @RequestBody IdDto idDto) {
        return ApiWrapMapper.success(userInfoService.deleteUser(idDto.getId()));
    }
}
